<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2017/12/24
 * Time: 下午6:53
 */

namespace Biz\module\Classes;

use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Biz\model\GoodsExtendInfo as mGoodsExtendInfo;
use Biz\model\OperateLog as mOperateLog;
use Biz\module\Classes\core\StatusAbstract;
use Biz\service\OperateLog as sOperateLog;


final class GoodsStatus extends StatusAbstract
{
    //操作列表常量
    const ACTION_MGT_ONLINE = 'mgtOnline';
    const ACTION_MGT_OFFLINE = 'mgtOffline';

    protected static $actionsDesc = [
        self::ACTION_MGT_ONLINE => '上架',
        self::ACTION_MGT_OFFLINE => '下架',
        self::ACTION_ADM_PASSED => '审核通过',
        self::ACTION_ADM_REJECT => '驳回',
        self::ACTION_ADM_OFFLINE => '下架'
    ];


    //后台管理员的操作
    const ACTION_ADM_OFFLINE = 'admOffline';
    const ACTION_ADM_PASSED = 'admPassed';
    const ACTION_ADM_REJECT = 'admReject';

    protected static $actionsAdminUrl = [
        self::ACTION_ADM_OFFLINE => 'admin/goods/ctrl/goods/status/' . mGoodsBasicInfo::STATUS_ADM_OFFLINE,
        self::ACTION_ADM_REJECT => 'admin/goods/ctrl/goods/status/' . mGoodsBasicInfo::STATUS_REJECT,
        self::ACTION_ADM_PASSED => 'admin/goods/ctrl/goods/status/' . mGoodsBasicInfo::STATUS_PASSED,
    ];

    protected static $statusAdminActionsMap = [
        mGoodsBasicInfo::STATUS_ONLINE => [self::ACTION_ADM_OFFLINE],
        mGoodsBasicInfo::STATUS_WAIT_AUDIT => [self::ACTION_ADM_PASSED, self::ACTION_ADM_REJECT],
    ];


    //商家上架商品
    public static function mgtOnline($uid, $shopId, $goodsId)
    {
        try {

            $info = mGoodsBasicInfo::getInfo($goodsId, ['status', 'goods_id', 'uid', 'shop_id', 'first_online_time'], true);

            if ($info['shop_id'] != $shopId) {
                throw new \Exception('此商品不是您店里的哦，请不要操作');
            }

            if (in_array($info['status'], [mGoodsBasicInfo::STATUS_REJECT, mGoodsBasicInfo::STATUS_ADM_OFFLINE, mGoodsBasicInfo::STATUS_WAIT_AUDIT,])) {
                throw new \Exception('送审中或已拒绝或运营下架中的商品不可自己操作上架');
            }

            $ret = mGoodsBasicInfo::action(function () use ($uid, $shopId, $goodsId, $info) {
                $operateTime = date('Y-m-d H:i:s');
                $data = [
                    'status' => mGoodsBasicInfo::STATUS_ONLINE,
                    'operate_time' => $operateTime,
                    'had_online' => mGoodsBasicInfo::HAD_ONLINE_YES
                ];
                if (strtotime($info['first_online_time']) <= strtotime('1971-01-01')) {//首次上架时间
                    $data['first_online_time'] = $operateTime;
                }
                $result = mGoodsBasicInfo::update($data, ['goods_id' => $goodsId]);

                if ($result->rowCount() == 0) {
                    throw new \Exception('状态修改失败');
                }

                sOperateLog::record(mOperateLog::TYPE_GOODS_MGT_CHANGE_STATUS, $uid, $goodsId, ['uid' => $uid, 'goods_id' => $goodsId, 'old_status' => mGoodsBasicInfo::$statusDesc[$info['status']]], ['new_status' => '上架']);

            });


        } catch (\Exception $e) {
            throw $e;
        }
    }

    //商家下架商品
    public static function mgtOffline($uid, $shopId, $goodsId)
    {
        try {

            $info = mGoodsBasicInfo::getInfoFromMaster($goodsId, ['status', 'goods_id', 'uid', 'shop_id']);

            if ($info['shop_id'] != $shopId) {
                throw new \Exception('此商品不是您店里的哦，请不要操作');
            }

            $ret = mGoodsBasicInfo::action(function () use ($uid, $shopId, $goodsId, $info) {
                $data = mGoodsBasicInfo::update(['status' => mGoodsBasicInfo::STATUS_OFFLINE, 'operate_time' => date('Y-m-d H:i:s'), 'operate_adminuid' => 0], ['goods_id' => $goodsId]);

                if ($data->rowCount() == 0) {
                    throw new \Exception('状态修改失败');
                }

                sOperateLog::record(mOperateLog::TYPE_GOODS_MGT_CHANGE_STATUS, $uid, $goodsId, ['uid' => $uid, 'goods_id' => $goodsId, 'old_status' => mGoodsBasicInfo::$statusDesc[$info['status']]], ['new_status' => '下架']);


            });


        } catch (\Exception $e) {
            throw $e;
        }
    }

    // 商家删除商品
    public static function mgtDelete($uid, $shopId, $goodsId)
    {
        try {

            $info = mGoodsBasicInfo::getInfo($goodsId, ['status', 'goods_id', 'uid', 'shop_id']);

            if ($info['shop_id'] != $shopId) {
                throw new \Exception('此商品不是您店里的哦，请不要操作');
            }

            $ret = mGoodsBasicInfo::action(function () use ($uid, $shopId, $goodsId, $info) {
                $data = mGoodsBasicInfo::update(['status' => mGoodsBasicInfo::STATUS_DELETE, 'operate_time' => date('Y-m-d H:i:s')], ['goods_id' => $goodsId]);

                if ($data->rowCount() == 0) {
                    throw new \Exception('状态修改失败');
                }

                sOperateLog::record(mOperateLog::TYPE_GOODS_MGT_CHANGE_STATUS, $uid, $goodsId, ['uid' => $uid, 'goods_id' => $goodsId, 'old_status' => mGoodsBasicInfo::$statusDesc[$info['status']]], ['new_status' => '删除']);


            });


        } catch (\Exception $e) {
            throw $e;
        }
    }

    //商家送审
    public static function mgtDelivered($uid, $shopId, $goodsId)
    {
        try {

            $info = mGoodsBasicInfo::getInfo($goodsId, ['status', 'goods_id', 'uid', 'shop_id']);

            if ($info['uid'] != $uid) {
                throw new \Exception('此商品不是您店里的哦，请不要操作');
            }

            $ret = mGoodsBasicInfo::action(function () use ($uid, $shopId, $goodsId, $info) {
                $data = mGoodsBasicInfo::update(['status' => mGoodsBasicInfo::STATUS_WAIT_AUDIT, 'operate_time' => date('Y-m-d H:i:s'), 'operate_adminuid' => 0], ['goods_id' => $goodsId]);

                if ($data->rowCount() == 0) {
                    throw new \Exception('状态修改失败');
                }

                sOperateLog::record(mOperateLog::TYPE_GOODS_MGT_CHANGE_STATUS, $uid, $goodsId, ['uid' => $uid, 'goods_id' => $goodsId, 'old_status' => mGoodsBasicInfo::$statusDesc[$info['status']]], ['new_status' => '送审']);


            });


        } catch (\Exception $e) {
            throw $e;
        }
    }

    //管理员上架商品
    public static function admOnline($goodsId, $adminUid, $remark)
    {
        try {

            $info = mGoodsBasicInfo::getInfo($goodsId, ['status', 'goods_id', 'uid', 'shop_id']);

            $ret = mGoodsBasicInfo::action(function () use ($goodsId, $adminUid, $remark, $info) {

                $data = mGoodsBasicInfo::update(['status' => mGoodsBasicInfo::STATUS_ONLINE, 'operate_time' => date('Y-m-d H:i:s'), 'operate_adminuid' => $adminUid], ['goods_id' => $goodsId]);

                if ($data->rowCount() == 0) {
                    throw new \Exception('状态修改失败-基本表');
                }

                $data = mGoodsExtendInfo::update(['remark' => $remark], ['goods_id' => $goodsId]);


                if ($data->rowCount() == 0) {
                    throw new \Exception('状态修改失败-扩展表');
                }

                sOperateLog::record(mOperateLog::TYPE_GOODS_ADM_CHANGE_STATUS, 0, $goodsId, ['goods_id' => $goodsId, 'old_status' => mGoodsBasicInfo::$statusDesc[$info['status']]], ['remark' => $remark, 'new_status' => '上架'], $adminUid);

            });

        } catch (\Exception $e) {
            throw $e;
        }
    }

    //管理员下架商品
    public static function admOffline($goodsId, $adminUid, $remark)
    {
        try {

            $info = mGoodsBasicInfo::getInfo($goodsId, ['status', 'goods_id', 'uid', 'shop_id']);

            $ret = mGoodsBasicInfo::action(function () use ($goodsId, $adminUid, $remark, $info) {

                $data = mGoodsBasicInfo::update(['status' => mGoodsBasicInfo::STATUS_ADM_OFFLINE, 'operate_time' => date('Y-m-d H:i:s'), 'operate_adminuid' => $adminUid], ['goods_id' => $goodsId]);

                if ($data->rowCount() == 0) {
                    throw new \Exception('状态修改失败');
                }

                $data = mGoodsExtendInfo::update(['remark' => $remark], ['goods_id' => $goodsId]);

                if ($data->errorCode() !== '00000') {
                    throw new \Exception('状态修改失败');
                }

                sOperateLog::record(mOperateLog::TYPE_GOODS_ADM_CHANGE_STATUS, 0, $goodsId, ['goods_id' => $goodsId, 'old_status' => mGoodsBasicInfo::$statusDesc[$info['status']]], ['remark' => $remark, 'new_status' => '下架'], $adminUid);

            });

        } catch (\Exception $e) {
            throw $e;
        }
    }

    //管理员驳回审核商品
    public static function admReject($goodsId, $adminUid, $remark)
    {
        try {

            //暂时产品需求  驳回=下架
            $remark = "(驳回)$remark";

            self::admOffline($goodsId, $adminUid, $remark);

        } catch (\Exception $e) {
            throw $e;
        }
    }

    //管理员审核通过商品
    public static function admPassed($goodsId, $adminUid, $remark)
    {
        try {

            //暂时产品需求  审核通过=上架
            $remark = "(审核通过)$remark";

            self::admOnline($goodsId, $adminUid, $remark);

        } catch (\Exception $e) {
            throw $e;
        }
    }

}